home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2276 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can't figure this out
  5. Date: 20 Jan 1996 04:09:43 GMT
  6. Organization: Los Alamos National Laboratory
  7. Distribution: world
  8. Message-ID: <TANMOY.96Jan19210943@qcd.lanl.gov>
  9. References: <31000091.3778302@news.panix.com> <19JAN199615253245@erich.triumf.ca>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: bennett@erich.triumf.ca's message of 19 Jan 1996 15:25 PST
  14.  
  15. --text follows this line--
  16. In article <19JAN199615253245@erich.triumf.ca> bennett@erich.triumf.ca
  17. (P.Bennett) writes: 
  18. <snip>
  19.    In article <31000091.3778302@news.panix.com>, dm@panix.com (Dan'l) writes...
  20.    >I am learning C and I have not had any problems understanding most
  21.    >concepts I have learned so far.  But to date I still can't figure out
  22.    >how the outcome of this program is 15.  Somehow one of the B's ends up
  23.    >a three and the other B a 5, or am I so off base that I can't see
  24.    >what's really happening.    Can someone please walk me through this
  25.    >one.                                             Thanks     Dan'l
  26.    > 
  27.    >#define A 3
  28.    >#define B A + A
  29.  
  30.    This makes B = "3 + 3", not 6 - #defines just do text substitution, not
  31.    arithmetic.
  32.  
  33.    >#define C B * B
  34.  
  35.    Now C = 3 + 3 * 3 + 3.  The multiplication will be done first, so you get 
  36.    3 + 9  + 3 = 15
  37.  
  38. This and a previous mail have both explained it slightly wrong. The
  39. error is that when you define something, the definition does not get
  40. expanded immediately: definitions are expanded only when used.
  41.  
  42. Thus, the correct way to state is that
  43.  
  44. #define A 3
  45. #define B A + A
  46. #define C B * B
  47.  
  48. stay as they are, but when you write C, the following happens
  49.  
  50. C gets replaced by 
  51.  
  52. B * B
  53.  
  54. This is rescanned to get first
  55.  
  56. A + A * B
  57.  
  58. The first part is rescanned and so on to get in succession
  59.  
  60. 3 + A * B
  61. 3 + 3 * B
  62. 3 + 3 * A + A
  63. 3 + 3 * 3 + A
  64. 3 + 3 * 3 + 3
  65.  
  66. The nett result is of course the same: in more complicated cases, it
  67. is better to remember what is going on.
  68.  
  69.    If you _really_ must do something like this, enclose the expressions in
  70.    parenthesis, like so:
  71.    #define A 3
  72.    #define B ((A) + (A))
  73.    #define C ((B) * (B))
  74.  
  75.    I think I have enough parentheses  there to ensure things are evaluated in the
  76.    right order...  :-)
  77.  
  78. Cheers
  79. Tanmoy
  80. --
  81. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  82. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  83. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  84. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  85. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  86. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  87.